创建爬虫----遍历单个域名爬取赶集网

爬取赶集网的页面链接

#赶集网首页静态页面的链接爬取
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

html=urlopen("http://ty.ganji.com/")
bsObj=BeautifulSoup(html)

for link in bsObj.find("div",{"class":"content-col"}).findAll("a",href=re.compile("^[a-z]")):
   if 'href' in link.attrs:
      print("http://ty.ganji.com/"+link.attrs['href'])

部分运行结果

#出现404HTTPError,修改请求头访问
from bs4 import BeautifulSoup
import requests
import re

session=requests.session()
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36",
         "Accept": "text / html, application / xhtml + xml, application / xml;q = 0.9, image / webp, * / *;q = 0.8"}
#def getlinks(articleUrl):
url="http://ty.ganji.com/fang1/"
req=session.get(url,headers=headers)
bsObj=BeautifulSoup(req.text)
for link in bsObj.find("div",{"class":"f-list js-tips-list"}).findAll("a",href=re.compile("^\/")):
     if 'href' in link.attrs:
         print("http://ty.ganji.com"+link.attrs['href'])

部分运行结果

好的,我可以为您提供一些关于爬取QQ空间相册的Python网络爬虫的相关信息。 首先,爬取QQ空间相册需要模拟登录QQ空间,获取相册的相册ID和相册密钥,然后根据相册ID和相册密钥构造相册访问链接,最后获取相册中的图片链接并下载保存。 以下是一个简单的爬取QQ空间相册的Python爬虫示例代码: ```python import requests from bs4 import BeautifulSoup # 模拟登录QQ空间获取cookies login_url = 'https://user.qzone.qq.com/' login_data = { 'username': 'your_username', 'password': 'your_password' } session = requests.Session() session.post(login_url, data=login_data) # 获取相册的相册ID和相册密钥 album_url = 'https://user.qzone.qq.com/proxy/domain/photo.qzone.qq.com/fcgi-bin/fcg_list_album_v3' album_params = { 'g_tk': 'your_gtk', 'uin': 'your_uin', 'hostUin': 'your_hostUin', 'format': 'json', 'inCharset': 'utf-8', 'outCharset': 'utf-8', 'notice': '0', 'numperpage': '30', 'pagenum': '0', 'callback': '_Callback', 'preCallback': '_Callback', 'json_esc': '1', 't': '0.9002957465610669' } album_response = session.get(album_url, params=album_params) album_json = album_response.content.decode('utf-8') album_data = eval(album_json.strip('_Callback(').rstrip(');')) album_list = album_data['data']['albumListModeSort'] # 遍历相册列表获取相册中的图片链接并下载保存 for album in album_list: album_id = album['id'] album_key = album['skey'] album_name = album['name'] album_url = 'https://user.qzone.qq.com/proxy/domain/photo.qzone.qq.com/fcgi-bin/cgi_list_photo' album_params = { 'g_tk': 'your_gtk', 'uin': 'your_uin', 'albumid': album_id, 'ps': '100', 'pagenum': '0', 'jsoncallback': 'shine0_Callback', 'callback': 'shine0', 'extral': 'undefined', 't': '0.9002957465610669' } album_response = session.get(album_url, params=album_params) album_json = album_response.content.decode('utf-8') album_data = eval(album_json.strip('shine0_Callback(').rstrip(');')) album_photo_list = album_data['data']['photoList'] for photo in album_photo_list: photo_url = photo['url'] photo_response = session.get(photo_url) with open(album_name + '/' + photo['name'], 'wb') as f: f.write(photo_response.content) ``` 请注意,为了保护您的账户安全,请替换示例中的您的用户名,密码,GTK,UIN和HostUin等参数。 希望这个示例可以帮助您开始爬取QQ空间相册。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值