- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试抓取法国网站 SeLoger,我可以找到并抓取所有广告并将其放入 Json 中。问题是我无法用这种方式找到广告的最终网址。该 Url 位于名为“cartouche”的 div 中,其类为 c-pa-link link_AB。
import requests
from bs4 import BeautifulSoup
import json
url = 'https://www.seloger.com/list.htm?tri=initial&enterprise=0&idtypebien=2,1&idtt=2,5&naturebien=1,2,4&ci=440109'
headers = {
'User-Agent': '*',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
s = requests.Session()
s.headers.update(headers)
r = s.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
for script_item in soup.find_all('script'):
if 'var ava_data' in script_item.text:
raw_json = script_item.text.split('var ava_data = ')[1].split('};')[0] + "}"
data = json.loads(raw_json)
print(data)
我希望像这样在 json 中放置一个字段。
{
"url":"https://www.seloger.com/annonces/achat/appartement/nantes-44/centre-ville/144279775.htm?enterprise=0&natures=1,4&places=%5b%7bci%3a440109%7d%5d&projects=2,5&qsversion=1.0&types=1,2&bd=ListToDetail",
"idannonce": "149546457",
"idagence": "294918",
"idtiers": "323172",
"typedebien": "Appartement",
"typedetransaction": [
"viager"
],
"idtypepublicationsourcecouplage": "SL",
"position": "2",
"codepostal": "44100",
"ville": "Nantes",
"departement": "Loire-Atlantique",
"codeinsee": "440109",
"produitsvisibilite": "AD:AC:BX:AW",
"affichagetype": [
{
"name": "liste",
"value": "True"
}
],
"cp": "44100",
"etage": "0",
"idtypechauffage": "0",
"idtypecommerce": "0",
"idtypecuisine": "séparée équipée",
"naturebien": "1",
"si_balcon": "1",
"nb_chambres": "1",
"nb_pieces": "2",
"si_sdbain": "0",
"si_sdEau": "0",
"nb_photos": "15",
"prix": "32180",
"surface": "41"
}
感谢您的帮助。
最佳答案
您可以使用 zip()
函数将产品从 json 数据“绑定(bind)”到网页中的 URL:
import requests
from bs4 import BeautifulSoup
import json
url = 'https://www.seloger.com/list.htm?tri=initial&enterprise=0&idtypebien=2,1&idtt=2,5&naturebien=1,2,4&ci=440109'
headers = {
'User-Agent': '*',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1'
}
s = requests.Session()
s.headers.update(headers)
r = s.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
for script_item in soup.find_all('script'):
if 'var ava_data' in script_item.text:
raw_json = script_item.text.split('var ava_data = ')[1].split('};')[0] + "}"
data = json.loads(raw_json)
for a, p in zip(soup.select('.c-pa-info > a'), data['products']):
p['url'] = a['href']
print(json.dumps(data, indent=4))
打印:
...
{
"idannonce": "139994713",
"idagence": "48074",
"idtiers": "24082",
"typedebien": "Appartement",
"typedetransaction": [
"vente"
],
"idtypepublicationsourcecouplage": "SL9",
"position": "16",
"codepostal": "44000",
"ville": "Nantes",
"departement": "Loire-Atlantique",
"codeinsee": "440109",
"produitsvisibilite": "AM:AC:BB:BX:AW",
"affichagetype": [
{
"name": "liste",
"value": true
}
],
"cp": "44000",
"etage": "0",
"idtypechauffage": "0",
"idtypecommerce": "0",
"idtypecuisine": "0",
"naturebien": "2",
"si_balcon": "0",
"nb_chambres": "0",
"nb_pieces": "3",
"si_sdbain": "0",
"si_sdEau": "0",
"nb_photos": "4",
"prix": "147900",
"surface": "63",
"url": "https://www.selogerneuf.com/annonces/achat/appartement/nantes-44/139994713/#?cmp=INTSL_ListToDetail"
},
{
"idannonce": "146486955",
"idagence": "334754",
...
注意:某些 URL 的结构与
不同https://www.seloger.com/annonces/achat/appartement/nantes-44/centre-ville/{idannonce}.htm?ci=440109&enterprise=0&idtt=2,5&idtypebien=2,1&naturebien=1,2,4&tri=initial&bd=ListToDetail
例如
https://www.selogerneuf.com/annonces/investissement/appartement/nantes-44/146486955/#?cmp=INTSL_ListToDetail
关于python - 有没有办法从 SeLoger 中抓取广告的网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57329121/
我正在通过 NodeSchool.io 练习学习 React 和 Express 框架。 我想将所有练习文件存储在具有多个页面的单个应用程序中,例如 索引 索引2 索引3 索引4 .... local
从这里:http://developer.android.com/reference/android/os/AsyncTask.html doInBackground(URL... urls) onP
我最近收到了一封电子邮件,其中包含以下内容(请勿点击!): UNS 这是原始电子邮件的链接:https://gist.github.com/anonymous/16963a230cab0a3a1bc
在 android 中,可以单击带有 URL 的 TextView 以在网络中打开 URL,方法是: android:autoLink="web" 我想做的是捕获这次点击,如果这个 TextView
我在我的网站上以 mysite.anotherdomain.org 的形式实现 Facebook 登录。我在 JavaScript SDK 的文档中做了所有解释,但由于我遇到了一些问题,我想知道错误是
我在 window.location.href 中有响应网址,我需要其中的 error、error_description 和 state 的值 http://localhost:4200/#erro
我正在创建无限加载,意味着当用户到达页面底部/特定 div 时会加载新页面。目前我有这个代码可以在点击时加载新页面。 $("#about").click(function(){ // load
当我们在谷歌引擎中搜索时,它也会显示热门网站标签或链接。就像我们搜索“bing”或“net beans”时一样。 问:它如何显示这些链接。我们是否必须告诉它显示这些链接。 问:它是否与 sitemap
我想从我的网址中获取我的产品。例如: http://www.website.com/product-category/iphone 我想获取 iphone,这对我的代码来说没问题,但我有一个下拉菜单来
我对 Pythonanywhere 完全陌生,我不知道为什么静态文件没有加载...这是我存储 css 和图像的路径,即 static/images/wikiLang.png 等 /static/adm
我正在使用这个正则表达式来验证 youtube 网址。 ^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$ 它很好用。 但我有这个
我刚刚在 gist.github 上传了一个我正在处理的小编码项目,因为它似乎是一次上传几个类的好方法。 我想将某人与我的“要点”联系起来,并在角落里写着: Public Clone URL: git
我正在使用 jQuery 验证引擎来解析我的表单数据: https://github.com/posabsolute/jQuery-Validation-Engine 验证 Twitter URL 的
我有一个 Django 应用程序,它可以在 localhost 上正常工作。即使对于 utf-8 URL 路径也是如此。但是当我在生产中使用它时,它给了我一个错误: 2019-09-01 14:32:
我已经安装了Laravel并开始尝试编写一个应用程序。我在/ app所在的目录中为 Assets 创建了一些目录。但是,当我尝试访问本地主机中的图像时,例如:http://localhost/asse
我们正在寻找一种方法来检查一长串 YouTube 网址,以查找目前私有(private)、已删除或不再可用的视频。我们可以检查状态,但即使视频不再公开可用,URL 也会返回 200。例如这两个: ht
我在 YouTube 上有现场事件,我想在我的网站上播放它。我想将我的事件设为私有(private),获取它的 RTMP 广播 URL 并将其粘贴到我的网站上,在 JWPlayer 中。 那可能吗?
当我在谷歌上搜索我的域时,它会显示我网站上的几个 https 网址,因为谷歌喜欢 https,但出于特殊原因我不想索引 https/ssl 版本。 如何避免这种情况,全世界都只通过 htaccess
我想获取在 Salesforce.com 授权期间作为回调收到的当前 URL。 url 中的数据位于片段部分。 最佳答案 您可以使用 $_SERVER['HTTP_HOST'] 和 $_SERVER[
我正在使用 ionic 创建一个应用程序,其中我使用 iframe 显示 URL。 这是 HTML 代码: 这是 Angular js: $scope.iframeHeight = windo
我是一名优秀的程序员,十分优秀!