#coding=utf-8

import time
import requests
from lxml import etree
from pymongo import MongoClient
from selenium import webdriver client = MongoClient("IP", 27017)
db = client["Automobile"]
collection = db["wenda_autohome"]
db.authenticate("","") driver = webdriver.Chrome(executable_path=r"D:\chromedriver_win32\chromedriver.exe") def splist(l, s):
return [l[i: i+s] for i in range(len(l)) if i%s==0] for i in range(36726, 40202):
# url = 'https://wenda.autohome.com.cn/topic/detail/40195'
url = 'https://wenda.autohome.com.cn/topic/detail/' + str(i) time.sleep(1)
driver.get(url)
html = driver.page_source
tree = etree.HTML(html) question = tree.xpath("//h1[@class='card-title']/text()")
answer_list = tree.xpath("//a[@class='text']/text()")
if question==[] or answer_list==[]:
continue n = 0
for j in answer_list: n += 1
answer_list[n-1] = j[41:-37]
if answer_list[n-1][-3:]!='...':
continue s = "//div[@class='card-reply-wrap'][" + str(n) + "]//a[@class='more']"
try:
driver.find_element_by_xpath(s).click() html_answer = driver.page_source
tree_answer = etree.HTML(html_answer)
answer_part = tree_answer.xpath("//div[@class='answer-content']/div/div[@class='ahe__area ahe__block ahe__text']/p/text()")
answer = ''
for item in answer_part:
answer += item answer_list[n-1] = answer
time.sleep(1)
driver.get(url)
except Exception as e:
print e
continue keywords = tree.xpath("//ul[@class='card-tag-list']/li/text()")
discription_list = tree.xpath("//div[@class='ahe__area ahe__block ahe__text']/p/text()")
discription = ''
for j in discription_list:
discription += j zancai = tree.xpath("//span[@class='js-praise-count']/text()")
zancai_list = splist(zancai, 2) dc = {}
dc['keywords'] = keywords
dc['question'] = question[0]
dc['discription'] = discription
dc['answer'] = answer_list
dc['zancai'] = zancai_list
dc['url'] = url collection.insert(dc) driver.close()

Python 爬取汽车领域问答语料(自用)的更多相关文章

  1. Python 爬取汽车之家口碑数据

    本文仅供学习交流使用,如侵立删!联系方式见文末 汽车之家口碑数据 2021.8.3 更新 增加用户信息参数.认证车辆信息等 2021.3.24 更新 更新最新数据接口 2020.12.25 更新 添加 ...

  2. 使用python爬取MedSci上的期刊信息

    使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...

  3. 毕设之Python爬取天气数据及可视化分析

    写在前面的一些P话:(https://jq.qq.com/?_wv=1027&k=RFkfeU8j) 天气预报我们每天都会关注,我们可以根据未来的天气增减衣物.安排出行,每天的气温.风速风向. ...

  4. Python 爬取所有51VOA网站的Learn a words文本及mp3音频

    Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...

  5. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  6. python爬取某个网页的图片-如百度贴吧

    python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...

  7. Python:爬取乌云厂商列表,使用BeautifulSoup解析

    在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...

  8. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...

  9. Python爬取豆瓣指定书籍的短评

    Python爬取豆瓣指定书籍的短评 #!/usr/bin/python # coding=utf-8 import re import sys import time import random im ...

随机推荐

  1. python版本管理(python环境隔离)

    这将是一篇比较短的文章. 我发文向来注重文章质量,营养不够的宁可不发,但是我相信很多人需要这篇文章. 之所以要去搞清楚这个问题,是我在把 vscode 的 inspector 设置为 pipenv 生 ...

  2. img加载不出来,给个默认图片。

    忽然发现,jq里也有坑,很多东西莫名其妙的被废弃了……所以,只能用原生js来做了: $('img').each(function() { if (!this.complete || typeof th ...

  3. lr中exit(-1)和return 0的区别

    LR脚本实践:关于lr中exit(-1)和return 0的区别 exit(-1):从当前action里面exit(-1)所在行,当前迭代里面直接退出来,终止运行: return 0:忽略当前acti ...

  4. Java Eclipse插件

    EasyExplore 快速打开文件所在目录1 http://sourceforge.net/projects/easystruts/ OpenExplorer 快速打开文件所在目录2 https:/ ...

  5. StreamingAssets文件夹的读取异常

    1.今天在读取StreamingAssets文件夹中的文本文件的时候,出现了异常,花了一个多小时解决了,把解决结果给大家梳理一下 2.文本文件夹所在位置:在StreamingAssets文件夹中新建一 ...

  6. EasyUI学习总结(一)——EasyUI入门(转载)

    本文转载自:http://www.cnblogs.com/xdp-gacl/p/4075079.html 一.EasyUI下载 EasyUI官方下载地址:http://www.jeasyui.com/ ...

  7. WQS二分题集

    WQS二分,一种优化一类特殊DP的方法. 很多最优化问题都是形如“一堆物品,取与不取之间有限制.现在规定只取k个,最大/小化总收益”. 这类问题最自然的想法是:设f[i][j]表示前i个取j个的最大收 ...

  8. Codeforces 850B

    题意: 给出一个序列,两种操作: 1.删除一个数,代价为x 2.给一个数+1,代价为y 求最小代价,使这个序列不为空,且所有的数的gcd>1 n<=5e5,a[i]<=1e6 其实思 ...

  9. 【c++随手记】编程基础之输入输出

    今天试了下noi oj的1.1节,随便总结一下. [cout左对齐右对齐的方法] #include<iostream> #include<cstdio> #include< ...

  10. hdu 4074 Darts

    思路:p[n][m][0]表示A为n,B为m,A为先手胜的概率:          p[n][m][1]表示A为n,B为m,B为先手胜的概率.          d[i]表示圆盘上数字的大小. 容易得 ...