python - 刮痧用美汤和 Selenium 问题

标签 python selenium web-scraping beautifulsoup

现在我正在这个网址https://www.lazada.com.my/products/xiaomi-mi-a1-4gb-ram-32gb-rom-i253761547-s336359472.html?spm=a2o4k.searchlistcategory.list.64.71546883QBZiNT&search=1上进行抓取

我想抓取所有产品评论,但出现错误.. 任何帮助我真的很感激谢谢你:)

我的代码

import requests
from selenium import webdriver
from bs4 import BeautifulSoup as soup
import time
from selenium.webdriver.chrome.options import Options


url = 'https://www.lazada.com.my/products/xiaomi-mi-a1-4gb-ram-32gb- rom-i253761547-s336359472.html? spm=a2o4k.searchlistcategory.list.64.71546883QBZiNT&search=1'

chrome_options = Options()
#chrome_options.add_argument("--headless")
browser = webdriver.Chrome('/Users/e5/fyp/chromedriver', 
chrome_options=chrome_options)
browser.get(url)
time.sleep(0.1)


d = soup(requests.get('https://www.lazada.com.my/products/xiaomi-mi-a1-4gb-ram-32gb-rom-i253761547-s336359472.html?spm=a2o4k.searchlistcategory.list.64.71546883QBZiNT&search=1').text, 'html.parser')
results = list(map(int, filter(None, [i.text for i in d.find_all('button', {'class':'next-pagination-item'})])))
print (results)
for i in range(min(results), max(results)+1):

    browser.find_element_by_xpath('//*[@id="module_product_review"]/div/div[3]/div[2]/div/div/button[{i}]').click()
    page_soups = soup(browser.page_source, 'html.parser')
    headline = page_soups.findAll('div',attrs={"class":"item-content"})

    for item in headline:
        top = item.div
        text_headlines = top.text
        print(text_headlines)

我的错误

InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[@id="module_product_review"]/div/div[3]/div[2]/div/div/button[{i}] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[@id="module_product_review"]/div/div[3]/div[2]/div/div/button[{i}]' is not a valid XPath expression.
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.17134 x86_64)

最佳答案

只需使用他们的 json api,不需要 selenium 或 BeautifulSoup。

import requests

count = 0
for i in range(3):
    count+=1
    url = ('https://my.lazada.com.my/pdp/review/getReviewList?'
        'itemId=253761547&pageSize=5&filter=0&sort=0&pageNo='+str(count))
    req = requests.get(url)
    data = req.json()
    for i in data['model']['items']:
        buyerName = i['buyerName']
        reviewContent = i['reviewContent']
        print(buyerName, reviewContent)

关于python - 刮痧用美汤和 Selenium 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52794200/

相关文章:

c#-4.0 - 无法按名称和 XPath 在 Selenium WebDriver 中找到元素

python - 使用带循环的漂亮汤在 Python 中制作 Webscrape 交互式图表

python - 我如何使用 mongodb 在 Pyramid 中创建单元测试?

python - 中心 Seaborn 颜色条标签

c# - Selenium - 具有透明代理的 MoveToElement()

python - 在 Selenium Python 中以编程方式设置 IE 缩放级别

python-3.x - Python 网络抓取遗漏了搜索对象列表中的一个元素

php - 什么是向导?

Python:如何装饰一个特殊的(dunder)方法

python - 搜索 html 标签时 python 中的贪婪正则表达式错误