爬虫作业

首先是谷歌主页的爬取

from pip._vendor import requests

print('访问谷歌网站 获取Response对象')
r = requests.get("http://www.google.cn")
x = 1
while x <= 20:
    print('' + str(x) + '次的返回状态打印:' + str(r.status_code))
    print('' + str(x) + '次的text()打印:' + str(r.text))
    print('' + str(x) + '次的text()属性长度打印:' + str(len(r.text)))
    print('' + str(x) + '次的content属性长度打印:' +str(len(r.content)))
    x += 1

爬取结果第一次:

 html页面

from bs4 import BeautifulSoup
import re
soup=BeautifulSoup("<head><title>菜鸟教程(runoob.com)</title></head><body><h1>我的第一个标题</h1><p id="frist">我的第一个段落。</p></body><tr><td>row 1,cell 1</td><td>row 1,cell 2</td></tr><tr><td>row 2,cell 1</td><td>row 2,cell 2</td></tr></body></table>","html.parser")
print(soup.head,"26")   
print(soup.body)      
print(soup.find_all(id="china"))  #打印id为china的文本
r=soup.text
pattern = re.findall(u'[\u1100-\uFFFDh]+?',r)
print(pattern)

 

posted @ 2020-12-29 11:41  蜕变667  阅读(78)  评论(0)    收藏  举报