Python爬虫:URL管理器及其实现方式、网页下载器、网页解析器原理及其实现原理!

2022-08-18 13:45:06 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

Python爬虫之URL管理器:

Python爬虫:URL管理器实现方式:

Python爬虫之网页下载器:

urllib2实现网页下载器的三种方法:

具体代码:

Python网页解析器:

例子:

代码语言:javascript复制
import requests
from bs4 import BeautifulSoup
def getHTMLText(url):
    try:
        r = requests.get(url, timeout = 30)
        r.raise_for_status()
        #r.encoding = 'utf-8'
        return r.text
    except:
        return ""
def getContent(url):
    html = getHTMLText(url)
    # print(html)
    soup = BeautifulSoup(html, "html.parser")
    title = soup.select("div.hd > h1")
    print(title[0].get_text())
    time = soup.select("div.a_Info > span.a_time")
    print(time[0].string)
    author = soup.select("div.qq_articleFt > div.qq_toolWrap > div.qq_editor")
    print(author[0].get_text())
    paras = soup.select("div.Cnt-Main-Article-QQ > p.text")
    for para in paras:
        if len(para) > 0:
            print(para.get_text())
            print()
    #写入文件
    fo = open("text.txt", "w ")
    fo.writelines(title[0].get_text()   "n")
    fo.writelines(time[0].get_text()   "n")
    for para in paras:
        if len(para) > 0:
            fo.writelines(para.get_text()   "nn")
    fo.writelines(author[0].get_text()   'n')
    fo.close()
    #将爬取到的文章用字典格式来存
    article = {
        'Title' : title[0].get_text(),
        'Time' : time[0].get_text(),
        'Paragraph' : paras,
        'Author' : author[0].get_text()
    }
    print(article)
def main():
    url = "http://news.qq.com/a/20170504/012032.htm"
    getContent(url);
main()

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/134735.html原文链接:https://javaforall.cn

0 人点赞