|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是源程序,没完善有错误
我准备在今日头条爬取街拍图片,爬取下来的网页准备用BeautifulSoup清洗出网址信息,然后把bs4类型的网页数据转化为字符串,然后再通过eval()转化为字典形式,images = result['rawData']['data']['img_url'],然后在字典中找到图片链接,打印。
我不知道我这个逻辑对不对,可能清洗的地方也有点问题,也可能其他的地方也有问题, 问一下大家有没有好办法,大哥们帮帮我
第一次解决问题,看的时候还请多多包涵,第一次,第一次,我自己ye'shi
- import requests
- from requests.exceptions import RequestException
- import json
- from bs4 import BeautifulSoup
- def get_page_index(page_num):
- params = {
- 'keyword': '街拍',
- 'pd': 'atlas',
- 'source': 'search_subtab_switch',
- 'dvpf': 'pc',
- 'aid': '4916',
- 'page_num': 'page_num',
- 'rawJSON': '1',
- 'search_id': '202110031811450101501322040B5D277C'
- }
- url = 'https://so.toutiao.com/search?'
- response = requests.get(url,params=params)
- try:
- if response.status_code == 200:
- return response.text
- return None
- except RequestException:
- print('索引失败')
- return None
- def parse_page_index(html):
- soup = BeautifulSoup(html, 'lxml')
- result = soup.select('#id-1315518853458256559 > div.d-block.position-relative.overflow-hidden.radius-m.cs-image-border.cs-image-wrapper > div > div > img')
- result= eval(result)
- print(type(result),result)
- html = json.loads(html)
- # images = result['rawData']['data']['img_url']
- # print(images)
- def main(page_num):
- html = get_page_index(page_num)
- parse_page_index(html)
- if __name__ == '__main__':
- for i in range(2):
- main(i)
复制代码
报错信息
- TypeError: eval() arg 1 must be a string, bytes or code object
复制代码
|
|