Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将字典拼凑成链接?_Python_Beautifulsoup - Fatal编程技术网

Python 如何将字典拼凑成链接?

Python 如何将字典拼凑成链接?,python,beautifulsoup,Python,Beautifulsoup,我正在为我的学校学习BS4,并想从链接锚中提取词典的内容。如何提取字典ctdata的内容 详情如下: 链接:a ct=“result\u offer\u content” 我试过了 for offers in soup.find_all("a", {'ct':'result_offer_content'}): offre = offers.find('ctdata') print(jobtitle) 但输出是“None…”,它将作为json读入,因为它位于jso

我正在为我的学校学习BS4,并想从链接锚中提取词典的内容。如何提取字典
ctdata
的内容

详情如下:

链接:
a ct=“result\u offer\u content”

我试过了

for offers in soup.find_all("a", {'ct':'result_offer_content'}):
   offre = offers.find('ctdata')
   print(jobtitle)

但输出是“None…”

,它将作为json读入,因为它位于json结构中。由于您没有提供完整的代码,我对
jobtitle
引用的内容有点困惑。而且,由于这里没有完整的代码,我只能提供一个通用的解决方案,因此您需要进行调整,但以下是您在中的阅读方式:

import json

json_str = '{"ad_id_solr":"1a7d243c3610c62012159b7c9d4e900382bbe446","ad_id_mongo":"","ad_segment_id":1723,"ad_partner":"wizbii.com_premium","ad_sector":"Ing\u00e9nierie","ad_subsector":"","ad_jobtitle":"Ing\u00e9nieur d\u00e9veloppeur","ad_company":"SII","ad_type":"exact","ad_position":1,"ad_locality":"Bordeaux"}'

json_dict = json.loads(json_str)
附加的

既然您已经提供了url,我就可以看到这个问题了。您想使用
.get()
而不是
。查找属性
'ctdata'

import json
import requests
import bs4


req = requests.get("https://www.jobijoba.com/fr/query/?what=data&where=Bordeaux&where_type=city%22")

soup = bs4.BeautifulSoup(req.text, 'html.parser')

offers = soup.find_all("a", {'ct':'result_offer_content'})

for offers in soup.find_all("a", {'ct':'result_offer_content'}):
    offre = offers.get('ctdata')

    json_dict = json.loads(offre)
    jobtitle = json_dict['ad_jobtitle']
    print (jobtitle)
输出:

Ingénieur développeur

Ingénieur développeur
Data Scientist
Data Scientist

Développeur big data


Data Scientist
Data Scientist

Ingénieur développeur
Data Scientist
Data Scientist
Data Scientist



Ingénieur décisionnel

Architecte
Data Scientist
Data Scientist
Data Scientist

Développeur informatique
Ingénieur développeur
Ingénieur développeur
Data Scientist
Data Scientist
Développeur big data
Data Scientist
Data Scientist
Ingénieur développeur
Data Scientist
Data Scientist
Data Scientist
Ingénieur décisionnel
Architecte
Data Scientist
Data Scientist
Data Scientist
Développeur informatique
有些标签上没有职务,因此您可以通过检查职务是否为空,基本上跳过这些标签/不打印这些标签:

import json
import requests
import bs4


req = requests.get("https://www.jobijoba.com/fr/query/?what=data&where=Bordeaux&where_type=city%22")

soup = bs4.BeautifulSoup(req.text, 'html.parser')

offers = soup.find_all("a", {'ct':'result_offer_content'})

for offers in soup.find_all("a", {'ct':'result_offer_content'}):
    offre = offers.get('ctdata')

    json_dict = json.loads(offre)
    jobtitle = json_dict['ad_jobtitle']
    if jobtitle != '':
        print (jobtitle)
输出:

Ingénieur développeur

Ingénieur développeur
Data Scientist
Data Scientist

Développeur big data


Data Scientist
Data Scientist

Ingénieur développeur
Data Scientist
Data Scientist
Data Scientist



Ingénieur décisionnel

Architecte
Data Scientist
Data Scientist
Data Scientist

Développeur informatique
Ingénieur développeur
Ingénieur développeur
Data Scientist
Data Scientist
Développeur big data
Data Scientist
Data Scientist
Ingénieur développeur
Data Scientist
Data Scientist
Data Scientist
Ingénieur décisionnel
Architecte
Data Scientist
Data Scientist
Data Scientist
Développeur informatique

最终,您将以json格式阅读它。您是否可以在此之前包含代码?在url中包含
请求。get()
,以及
soup=BeautifulSoup()
等。这样更容易准确地看到您正在谈论的数据响应。您可以显示您试图刮取的实际HTML或至少是您正在刮取的网页吗?谢谢。这是我的培训页面:req=requests.get(“)谢谢。我添加了以下代码:for offers in soup.find_all(“a”),{ct':'result_offer_content'}):jobtitle=json.loads(offers.find('ctdata'))print(jobtitle)我仍然有一个类型错误,因为非类型“You want to use.get()not.find for the attribute'ctdata'”