【求助】selenium+chromedriver爬取微博问题

逆夏桑 2019-01-28 02:01:15

from selenium import webdriver
import time
import csv

def login():
driver.get("https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=https%3A%2F%2Fm.weibo.cn%2Fdetail%2F4332476089824509")
driver.implicitly_wait(5)
user_name = driver.find_element_by_id("loginName")
user_name.send_keys("xxxxxx")
user_psd = driver.find_element_by_id("loginPassword")
user_psd.send_keys("xxxxxx")
submit = driver.find_element_by_id("loginAction")
submit.click()
print("准备登录。。")
time.sleep(9)

def data_write(info_list):
with open("weibo_content.csv","a",encoding="gbk") as f:
csvfile = csv.writer(f)
csvfile.writerow(info_list)

def spider():
driver.get("https://m.weibo.cn/")
time.sleep(6)
all_messages = driver.find_elements_by_xpath("//div[@class='pannelwrap']/div[@class='wb-item-wrap']")
#找到微博内容框框
for each_frame in all_messages:
time.sleep(2)
pub_id = each_frame.find_element_by_xpath("header[@class='weibo-top m-box']/div[2]/div[1]/a/h3")[0].text.strip()
pub_url = "https://m.weibo.cn/" + each_frame.find_element_by_xpath("header[@class='weibo-top m-box']/div[2]/div[1]/a")[0].get_attribute("href")
pub_weibo = each_frame.find_element_by_xpath("article[@class='weibo-main']/div[1]/div[1]/div[1]")[0].text.strip()
info_list = [pub_id,pub_url,pub_weibo]
data_write(info_list)
print("正在写入:",pub_id)

if __name__ == "__main__":
#初始化selenium
driver = webdriver.Chrome()

login()

while True:
spider()
time.sleep(300)


报错的语句是:pub_id = each_frame.find_element_by_xpath("header[@class='weibo-top m-box']/div[2]/div[1]/a/h3")[0].text.strip()
报错的内容是can not locate pub_id,或者是AttributeError: 'list' object has no attribute 'find_element_by_xpath'
咋解决啊?感谢大大们
...全文
163 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yshuise 2019-01-28
  • 打赏
  • 举报
回复
先下载一个xpath工具,然后自己试试是否有这个内容。一般错误就是表达式错误或是没有这个内容

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧