python爬取百度域名_Python 爬虫练习(三) 利用百度进行子域名收集

不多介绍了,千篇一律的正则匹配.....

import requests

import re

head = {'User-Agent':

'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36'}

key = 'jcrcw.com' # 这里填主域名

lst = []

match = 'style="text-decoration:none;">(.*?)'

for i in range(1, 20): # 1-19页

url = "https://www.baidu.com/s?wd=inurl:{}&pn={}&oq={}&ie=utf-8".format(key, i, key)

print(url)

# response = requests.get(url,headers=head,cookies = cook).content

response = requests.get(url, headers=head).content

subdomains = re.findall(match, response.decode())

for j in subdomains:

j = j.replace('', '')

if key in j:

if j not in lst:

lst.append(j)

# print(lst)

print(lst)

运行结果:

f74270fe64ed2426b2de89a1079f919b.png

好的,这是一个很不错的爬虫入门练习。首先,你需要安装 Python 的 requests 和 BeautifulSoup 库。安装方法可以在官方文档中找到。 接下来,你需要分析链家网二手房信息的页面结构,找到需要爬取的数据。可以使用 Chrome 开发者工具来查看网页源代码和网络请求。通常情况下,你需要模拟浏览器发送请求,获取网页内容,然后使用 BeautifulSoup 解析 HTML,提取数据。 以下是一个简单的示例代码,用于爬取链家网二手房信息: ```python import requests from bs4 import BeautifulSoup url = 'https://bj.lianjia.com/ershoufang/' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') house_list = soup.find_all('div', class_='info') for house in house_list: title = house.find('div', class_='title').a.text.strip() address = house.find('div', class_='address').div.text.strip() price = house.find('div', class_='price').span.text.strip() print(title, address, price) ``` 在这个示例中,我们首先发送一个 GET 请求到链家网二手房信息的页面。然后使用 BeautifulSoup 解析 HTML,获取每个房源的标题、地址和价格信息。最后打印这些信息。 当然,这只是一个简单的示例代码,你可以根据自己的需要进行修改和调整。同时,需要注意的是,爬取网站数据是需要遵守相关法律法规和网站的使用协议的。在爬取数据之前,请先了解相关规定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值