开发者社区> 问答> 正文

Selenium代码无法清除堆栈溢出

我正在用pythonselenium构建一个网络抓取工具。该脚本抓取了诸如Amazon和Flipcart之类的网站,但无法抓取堆栈溢出和时尚。它总是向我返回一个空白的.csv文件。

这是我的堆栈oveflow代码:

from selenium import webdriver 
from selenium.webdriver.chrome.options import Options 
import pandas as pd 

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) \ Chrome/80.0.3987.132 Safari/537.36' 
driver_exe = 'chromedriver' 

options = Options() 
options.add_argument("--headless") 
options.add_argument(f'user-agent={user_agent}') 
options.add_argument("--disable-web-security") 
options.add_argument("--allow-running-insecure-content") 
options.add_argument("--allow-cross-origin-auth-prompt") 
options.add_argument("--disable-cookie-encryption") 

driver = webdriver.Chrome(executable_path=r"C:\Users\intel\Downloads\Setups\chromedriver.exe", options=options) 
driver.get("https://stackoverflow.com/") 

class_Name = "." + "question-hyperlink" 
x = driver.find_elements_by_css_selector(class_Name.replace(' ','.')) 
web_content_list = []

for i in x: 
    web_content_dict = {} 
    web_content_dict["Title"] = i.text 
    web_content_list.append(web_content_dict)

df = pd.DataFrame(web_content_list) 
df.to_csv(r'C:\Users\intel\Desktop\data_file.csv', index=False, mode='a', encoding='utf-8')

上面的stackovrflow代码通过在链接中添加/ questions来解决。

  • OFashion网站的代码*

    from selenium import webdriver from selenium.webdriver.chrome.options import Options import pandas as pd import time

    user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) '
    'Chrome/80.0.3987.132 Safari/537.36'

    driver_exe = 'chromedriver' options = Options() #options.add_argument("--headless") options.add_argument(f'user-agent={user_agent}') options.add_argument("--disable-web-security") options.add_argument("--allow-running-insecure-content") options.add_argument("--allow-cross-origin-auth-prompt")

    driver = webdriver.Chrome(executable_path=r"C:\Users\intel\Downloads\Setups\chromedriver.exe", options=options) driver.get("https://www.ofashion.com.cn/goods/10001?t=15777838840003") class_Name = "." + "content-par" x = driver.find_elements_by_css_selector(class_Name.replace(' ','.')) web_content_list = []

    for i in x: web_content_dict = {} web_content_dict["Title"] = i.text web_content_list.append(web_content_dict)

    df = pd.DataFrame(web_content_list) df.to_csv(r'C:\Users\intel\Desktop\data_file.csv', index=False, mode='a', encoding='utf-8')

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 21:36:22 554 0
1 条回答
写回答
取消 提交回答
  • 这是您的问题-您尚未登录。StackOverflow仅在您登录时向您显示问题-因此,如果您进行登录,则程序单击“登录”按钮,输入用户名和密码,然后提交,代码将从那里开始。(我通过删除无头属性发现了这一点)

    回答来源:stackoverflow

    2020-03-24 21:36:28
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载