我已经创建了一个python脚本,使用requests和BeautifulSoup来解析概要文件名以及从网页到它们的概要文件名的链接。内容似乎是动态生成的,但它们出现在页面源中。所以,我尝试了以下几点,但不幸的是我一无所获。你知道吗
我目前的尝试:
import requests
from bs4 import BeautifulSoup
URL = 'https://www.century21.com/real-estate-agents/Dallas,TX'
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9,bn;q=0.8',
'cache-control': 'max-age=0',
'cookie': 'JSESSIONID=8BF2F6FB5603A416DCFBAB8A3BB5A79E.app09-c21-id8; website_user_id=1255553501;',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
}
def get_info(link):
res = requests.get(link,headers=headers)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select(".media__content"):
profileUrl = item.get("href")
profileName = item.select_one("[itemprop='name']").get_text()
print(profileUrl,profileName)
if __name__ == '__main__':
get_info(URL)
How can I fetch the content from that page?
看起来你也可以构建这个url(尽管抓取它似乎更容易)
页面内容不是通过javascript呈现的。你的密码对我来说没问题。 您在查找profileUrl和处理
nonetype
异常方面遇到了一些问题。您必须关注a
标记才能获得数据你应该试试这个:
输出:
所需内容在页面源中不可用。当使用相同的
user-agent
发出请求时,站点非常擅长丢弃请求。因此,我使用fake_useragent
随机地为请求提供相同的内容。如果你不经常使用它,它就会起作用。你知道吗工作方案:
部分输出:
相关问题 更多 >
编程相关推荐