python网络爬虫基础模块安装

python网络爬虫基础模块安装


python的网络爬虫一般需要requests模块,urllib,urllib2, urllib3和bs4这几个模块,其中urllib和urllib2在安装python的时候就已经安装好了,所以直接查找其他模块即可

https://pan.baidu.com/s/1mhU9RPu(https://github.com/chenyonganyue/cya)这是python的一些包,找到Lib文件,若安装模块的时候提示缺少什么模块就直接去查找相应的文件,并放到python文件夹的lib里面
例如缺少bs4模块,直接取出bs4开头的文件

python网络爬虫基础模块安装_第1张图片
6O$`A}VDU$O$M6W$BEEMQUU.png

1.安装requests模块

pip install requests

安装完成,打开终端

import requests

若没报错,则安装完成

2.安装urllib3模块

pip install urllib3

安装完成,打开终端

import urllib3

若没报错,则安装完成

3.安装bs4模块

pip install bs4

安装完成,打开终端

import bs4

若没报错,则安装完成

附上自己写的一段简单的代码:

#coding: utf-8
import requests
import urllib
import re
import time
from bs4 import BeautifulSoup

gjc = urllib.quote('李')
url = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+gjc+'&json=1&p=3&sid=&csor=2&pwd= &cb=jQuery110207361392755424963_1505220177752&_=1505220177757'
proxies = {"http":'http://119.5.0.53', "http":'http://140.250.170.110', "http":'http://221.229.46.81'}
headers = {'GET':url,
            'HOST':'sp0.baidu.com',
            'Referer':'https://www.baidu.com/?tn=91694651_hao_pg',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'
            }
html = requests.get(url, headers=headers, proxies=proxies).content
soup = BeautifulSoup(html, 'html.parser')
res = soup.get_text()
keyword = re.findall("\"(.*?)\"", res)
num = 0
for i in keyword:
    num += 1
    if i == "s":
        for item in keyword[num:]:
            print item

你可能感兴趣的:(python网络爬虫基础模块安装)