requests_html 包爬虫求助 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
334862132
V2EX    Python

requests_html 包爬虫求助

  •  
  •   334862132 2019-09-29 10:25:22 +08:00 3156 次点击
    这是一个创建于 2212 天前的主题,其中的信息可能已经有所发展或是发生改变。
    今天学习 requests_html 包 网上找俩个爬虫 分别爬慕课网和 51cto 的爬虫 结果发现 传入的时候一个可以直接传 res.html,另一个必须要用 PyQuery 转换一下 res.text 才能传入 这个是为什么啊 上代码。。。。
    抓取 51 的
    from requests_html import AsyncHTMLSession # 导入异步模块

    asession = AsyncHTMLSession()

    BASE_URL = "http://edu.51cto.com/courselist/index-p{}.html"

    async def get_html():
    for i in range(1,2):
    r = await asession.get(BASE_URL.format(i)) # 异步等待
    get_item(r.html)

    def get_item(html):
    c_list = html.find('.cList',first=True)
    if c_list:
    items = c_list.find('.cList_Item')
    for item in items:
    title = item.find("h3",first=True).text # 课程名称
    href = item.find('h3>a',first=True).attrs["href"] # 课程的链接地址
    dict = {
    "title":title,
    "href":href,
    }
    print(dict)


    if __name__ == '__main__':
    result = asession.run(get_html)

    抓取慕课的

    from requests_html import AsyncHTMLSession
    from pyquery import PyQuery as pq

    s = AsyncHTMLSession()
    url = "https://www.imooc.com/course/list?page={i}"

    async def get_html():
    for i in range(1,2):
    res = await s.get(url.format(i=i))
    d = pq(res.text)
    get_content(d)

    def get_content(d):
    courses = d.items(".course-card-container")
    for course in courses:
    title = course.find(".course-card-name").text() # 查找 title
    des = course.find(".course-card-desc").text()

    dict = {
    "title":title,
    "des":des
    }
    print(dict)

    if __name__ == '__main__':
    ret = s.run(get_html)
    fifa666
        1
    fifa666  
       2019-09-29 11:05:10 +08:00
    你打印输出,看一下呗
    334862132
        2
    334862132  
    OP
       2019-09-29 11:11:16 +08:00
    @fifa666 主要打印输出没看出来有什么区别 刚在别人指点下又用 requests 包试了一下 俩个地址返回的都是 html 不是 json 我一下就懵逼了。。。
    silencefent
        3
    silencefent  
       2019-09-29 11:15:21 +08:00
    不要用 requests_html,就用 requests
    334862132
        4
    334862132  
    OP
       2019-09-29 11:53:33 +08:00
    @silencefent requests_html 封装 requests 自带异步多线程 用 requests 还要封装多线程 还要搞 i/o 异步,多麻烦,本来这个我也搞出来了,就是想不明白为什么传的东西不一样...
    334862132
        5
    334862132  
    OP
       2019-09-29 11:55:17 +08:00
    51 的 html 打印出来是这个 <HTML url='https://edu.51cto.com/courselist/index-p1.html'>
    慕课的 html 打印出来是 <HTML url='https://www.imooc.com/course/list?page=1'>
    sherlockwhite
        6
    sherlockwhite  
       2019-09-29 13:38:19 +08:00
    “多麻烦”?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2526 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 11:17 PVG 19:17 LAX 04:17 JFK 07:17
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86