asm*_*itu 1 python beautifulsoup web-scraping python-3.x python-requests
我正在尝试使用请求模块在 python 中创建一个脚本,以从网站上抓取不同工作的标题。要解析不同工作的标题,我需要首先从该站点获得相关响应,以便我可以使用 BeautifulSoup 处理内容。但是,当我运行以下脚本时,我可以看到该脚本产生的乱码实际上不包含我要查找的标题。
网站链接( In case you don't see any data, make sure to refresh the page)
我试过:
import requests
from bs4 import BeautifulSoup
link = 'https://www.alljobs.co.il/SearchResultsGuest.aspx?'
query_string = {
'page': '1',
'position': '235',
'type': '',
'city': '',
'region': ''
}
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
s.headers.update({"Referer":"https://www.alljobs.co.il/SearchResultsGuest.aspx?page=2&position=235&type=&city=®ion="})
res = s.get(link,params=query_string)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select(".job-content-top [class^='job-content-top-title'] a[title]"):
print(item.text)
Run Code Online (Sandbox Code Playgroud)
我什至这样试过:
import urllib.request
from bs4 import BeautifulSoup
from urllib.parse import urlencode
link = 'https://www.alljobs.co.il/SearchResultsGuest.aspx?'
query_string = {
'page': '1',
'position': '235',
'type': '',
'city': '',
'region': ''
}
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36",
"Referer":"https://www.alljobs.co.il/SearchResultsGuest.aspx?page=2&position=235&type=&city=®ion="
}
def get_content(url,params):
req = urllib.request.Request(f"{url}{params}",headers=headers)
res = urllib.request.urlopen(req).read()
soup = BeautifulSoup(res,"lxml")
for item in soup.select(".job-content-top [class^='job-content-top-title'] a[title]"):
yield item.text
if __name__ == '__main__':
params = urlencode(query_string)
for item in get_content(link,params):
print(item)
Run Code Online (Sandbox Code Playgroud)
如何使用请求获取不同工作的标题?
PS 浏览器模拟器不是这里完成任务的选项。
我想看看你的胡言乱语是什么样的。当我运行你的代码时,我得到了一堆希伯来语字符(这并不奇怪,因为网站是希伯来语)和职位:
????? ????? ??????, IT 项目经理?????? AllStars-IT Group (MT) ???? ???????????????/? 爪哇???????? ???????? ???????!??????????????????/? ????????????? /? ??????????? ????? ??????????? ???/???? ????? ??????????? ????? ????????? ??????????? ??????DBA SQL/ORACLE ?????? CPS 工作 ?????? /?? ???/???? ????? ?? ????? ??????????, ??? ????? ????????? /?? ????????????????? ???/? SAP ABAP ?????? ????? ???????????? ???/? 数据分析总监?????? ????????? 全栈开发人员????SQLink ???? /? ?????????????? ????? 毕???????? ????????” ? ???/? ?????????????? /? ????? 毕?????????? ???????????????????? ???/? ??????? /?? 阿巴??????????? ????? ????? ???/? ??????? /?? ??????????? ????? ???????? ???????????????????? 塔尔多????????????/? ???/? ????????? ??????SQLink ???? /? ??????全栈 ?????/? 高级软件工程师经理高级软件工程师高级嵌入式软件工程师嵌入式软件工程师高级软件工程师子公司PMM经理??????? /?? 后端全栈/前端软件工程师 软件验证工程师 首席产品经理 量子算法研究实习生 首席/高级检测团队首席支持工程师 软件工程师 工程师经理 高级软件工程师 高级嵌入式软件工程师 嵌入式软件工程师 高级软件工程师 子公司 PMM 经理 ????????? /?? 后端全栈/前端软件工程师 软件验证工程师 首席产品经理 量子算法研究实习生 首席/高级检测团队首席支持工程师 软件工程师 工程师经理 高级软件工程师 高级嵌入式软件工程师 嵌入式软件工程师 高级软件工程师 子公司 PMM 经理 ????????? /?? 后端全栈/前端软件工程师 软件验证工程师 首席产品经理 量子算法研究实习生 首席/高级检测团队首席支持工程师 软件工程师
您的问题是要过滤掉希伯来字符吗?因为那只需要简单的正则表达式!导入 re 包,然后用这个替换你的打印语句:
print(re.sub('[^A-z0-9]+',' ',item.text))
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!