requests_html

https://requests.readthedocs.io/projects/requests-html/en/latest/

非常好的教程:https://www.jcchouinard.com/web-scraping-with-python-and-requests-html/#Extract_the_Title_From_the_Page

from requests_html import HTML, HtmlElement, HTMLResponse, HTMLSession
def article_parse():
    session = HTMLSession()
    #新闻首页(list页面)
    r = session.get('https://www.ctdf.org.cn/article/category/news-2')

    '''
    node = r.html.find('#about', first=True',containing=None )  #查第一个element,否则返回list,设定contain则只会返回包含指定文本的Element对象
    node_text = node.text   #获取文本
    node_links = node.absolute_links   #内部所有的绝对Link
    node_attrs = node.attrs  #获取所有属性
    ode_html = node.html  #获取其html
    node.find('a')        #获取其内部的特定子Element对象,返回List
    node.html.search('something{}xxx')[0]  # 所有匹配可以设定开头和结尾中间的大括号里的内容,就是要取出来的 
    

    '''
    find里面用的css选择器例子:
        a
        a.someClass
        a#someID
        a[target=_blank]

 

posted @ 2023-09-12 14:30  花生与酒  阅读(13)  评论(0编辑  收藏  举报