码迷,mamicode.com
首页 > 其他好文 > 详细

Beautifulsoup分解

时间:2017-11-16 20:48:05      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:rom   lis   open   head   访问   页面   www.   soup   gen   

from urllib.request import Request, ProxyHandler
from urllib.request import build_opener
from bs4 import BeautifulSoup
import redis
urlfront = "http://www.xicidaili.com"
url = "http://www.xicidaili.com/nn/1"
r = redis.Redis(host=127.0.0.1, port=6379,db=0)

# def spider_IP(url):
# 获取整个页面
def get_allcode(url):
    # 设置代理IP
    proxy = {https: 110.73.0.45:8123}
    proxy_support = ProxyHandler(proxy);
    opener = build_opener(proxy_support)
    # 设置访问http协议头,模拟浏览器
    opener.addheaders = [
        (User-agent, Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6)]
    r = opener.open(url)
    html = r.read().decode("UTF-8");
    # print(html)
    return str(html)

# 根据URl用beautifulsoup提取,可以写方法
def find_ip(s):
    soup = BeautifulSoup(s, html.parser);
    aList = soup.find_all(name="tr",class_="odd")
    for items in aList:
        link = items.find_all("td")
        print("%s:%s" %(link[1].get_text(),link[2].get_text()))
find_ip(get_allcode(url))

 

Beautifulsoup分解

标签:rom   lis   open   head   访问   页面   www.   soup   gen   

原文地址:http://www.cnblogs.com/qieyu/p/7846085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!