'''python3
爬取网站:https://www.nihaowua.com/
爬取内容:撩妹金句
python库: requests + pyquery
'''
import requests
from pyquery import PyQuery as pq
url = 'https://www.nihaowua.com/'
headers = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/76.0.3809.87 Chrome/76.0.3809.87 Safari/537.36'
}
def spiders():
re = requests.get(url=url, headers=headers) # 爬取网页
html = pq(re.text)
text = html('section').text() # 解析网页:section 节点 -> 取节点的文本内容
return text
def main():
count = 0
while count < 100:
text = spiders()
print('第{}次爬取:{}'.format(count+1, text))
count += 1
if __name__ == '__main__':
main()
- 执行结果