Поиск и поиск результатов на веб-странице с использованием красивого супа - PullRequest
0 голосов
/ 20 января 2020

У нас есть требование удалить данные из Google AdBar. Я использую python для этого, но результаты получаются не так, как ожидалось. Может ли кто-нибудь помочь мне с этим.

Требуется получить такие результаты, как название продукта, название компании, их относительное положение и URL продукта с веб-страницы.

Используя приведенный ниже скрипт, он дает не 100% хорошие результаты, так как я новичок в Python Может ли кто-нибудь поправить меня или дать мне некоторые рекомендации по этому поводу?

Конечным результатом должно быть название продукта URL Позиция Компания

from bs4 import BeautifulSoup
import requests


#Grab and open URL, create BeatifulSoup object
url = "https://www.google.com/search?&q=iphone+11"
headers={
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
page = requests.get(url,headers=headers)
soup = BeautifulSoup(page.text,'html.parser')
#Grab Table of Contents
grab_toc = soup.find('div', {"class":"Yi78Pd"})

#Look for all divs with class: li
ftext = grab_toc.findAll('div', {"class":"mnr-c pla-unit"})
#Look for links
links = grab_toc.findAll('a',href=True)

#Iterate
for everytext in ftext:
    text = ''.join(everytext.findAll(text=True))
    data = text.strip()
    print (data)
    print("\n")

for everylink in links:
    print (everylink['href'])
    print("\n")

С селеном

 from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options

# prepare the option for the chrome driver
#webdriver.Chrome('/Users/user_123/downloads/chrome_driver')
options = webdriver.Chrome(Options)
options.add_argument('headless')

# start chrome browser
browser = webdriver.Chrome(chrome_options=options)
browser.get('http://www.google.com/iphone 11')
print(browser.current_url)
soup = BeautifulSoup(browser.page_source, 'lxml')
name_link = soup.find_all('div', class_='mnr-c pla-unit')
link = soup.find_all('href')
for n_link, l in zip(name_link,link):
    print(f'{n_link.text}\n{l.text}')
    print('---------')

Исключения

Traceback (most recent call last):
  File "/Users/user_123/Documents/selenium_with_beautiful_soup.py", line 6, in <module>
    options = webdriver.Chrome(Options)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1436, in _execute_child
    executable = os.fsencode(executable)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 809, in fsencode
    filename = fspath(filename)  # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not type

enter image description here enter image description here u

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...