Python什么时候用到字典


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

# coding:utf-8

 

import requests

from bs4 import BeautifulSoup

 

 

class SpiderProxy(object):

    #Python版本为2.7以上

    headers = {

        "Host": "www.xicidaili.com",

        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0",

        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",

        "Accept-Language": "en-US,en;q=0.5",

        "Accept-Encoding": "gzip, deflate",

        "Referer": "http://www.xicidaili.com/wt/1",

    }

 

    def __init__(self, session_url):

        self.req = requests.session()

        self.req.get(session_url)

 

    def get_pagesource(self, url):

        html = self.req.get(url, headers=self.headers)

        return html.content

 

    def get_all_proxy(self, url, n):

        data = []

        for i in range(1, n):

            html = self.get_pagesource(url + str(i))

            soup = BeautifulSoup(html, "lxml")

 

            table = soup.find('table', id="ip_list")

            for row in table.findAll("tr"):

                cells = row.findAll("td")

                tmp = []

                for item in cells:

 

                    tmp.append(item.find(text=True))

                data.append(tmp[1:3])

        return data

 

session_url = 'http://www.xicidaili.com/wt/1'

url = 'http://www.xicidaili.com/wt/'

p = SpiderProxy(session_url)

proxy_ip = p.get_all_proxy(url, 10)

for item in proxy_ip:

    if item:

        print item

更多Python相关技术文章,请访问Python教程栏目进行学习!

以上就是Python什么时候用到字典的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python文件打开的访问模式有哪些?

Python如何查看父类

Python基于opencv的图像压缩算法实例分析

Python 文件夹遍历和文件查找的实例

Python之configparser配置文件详解

Python中什么是对象

Python迭代器和for循环区别

Python 实用函数进阶(更新中)

Python中的pop函数和append函数详解

Python类是什么

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...