一个简单的python爬虫

如果想爬百度百科、知乎这一类的,首先要加一个文件头。然后使用requests.get时一起交过去。

# CrawUniRanking.py

import requests
import urllib3
from bs4 import BeautifulSoup

allUniv = []

def getHTMLText(url):
    try:
        header = {"User-Agent": "Mozilla/5.0 (W\
        ndows NT 10.0; Win64; x64) AppleWebKit/537.\
        36 (KHTML, like Gecko) Chrome/51.0.2704.106 Sa\
        fari/537.36"}
        #headers用于欺骗网站
        r = requests.get(url, headers=header)
        r.raise_for_status()
        r.encoding = 'utf-8'
        return r.text

    except:
        return ""

def fillUnivList(soup):
    data = soup.find_all('tr')
    for tr in data:
        ltd = tr.find_all('td')
        if len(ltd) == 0:
            continue
        singleUniv = []
        for td in ltd:
            singleUniv.append(td.string)
        allUniv.append(singleUniv)

def printUnivList(num):

    for i in range(num):
        u = allUniv[i]
        print("{1:^2} {2:{0}^20} {3:{0}^30} {4:{0}^20} {5:{0}^10} {6:{0}^10}".format(chr(12288), u[0], u[1], u[2], u[3], eval(u[4]), u[5])

def main(num):
    url='https://baike.baidu.com/item/%E4%B8%96%E7%95%8C100%E6%89%80%E8%91%97%E5%90%8D%E5%A4%A7%E5%AD%A6%E6%8E%92%E8%A1%8C%E6%A6%9C/12680287?fr=aladdin'
    html = getHTMLText(url)
    soup = BeautifulSoup(html, "html.parser")
    fillUnivList(soup)
    printUnivList(num)

main(10)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值