やりたいこと
href
のリンクを取得するのには成功したのですが、href
の中身が/projects/python/
で始まるリンクに絞り込むにはどうしたら良いでしょうか。
- スクレイピング対象: freelancer.com
コード
# -*- coding: utf-8 -*- from bs4 import BeautifulSoup import requests import csv import warnings warnings.filterwarnings('ignore') r = requests.get("https://www.freelancer.com/archives/python/2018-40/") soup_content = BeautifulSoup(r.content, "html.parser") f1 = open('r.txt', 'w') f1.write(r.text) f1.close soup = BeautifulSoup(r.text) with open('allhref.csv', 'w+',newline='',encoding='utf-8') as f: writer = csv.writer(f, lineterminator='\n') for link in soup.find_all('a'): writer.writerow([link.get('href')])
get('href')の中身
全てではありませんが、主なもののみ
/info/how-it-works /jobs/ /post-project /projects/python/online-web-tool/ /projects/php/Project-for-Kseniia-17867039/
r.textの中身
全てではありませんが、主なもののみ
<a title="Project for Kseniia I. -- 18/09/30 05:09:30 Job" href="/projects/php/Project-for-Kseniia-17867039/" class="job">Project for Kseniia I. -- 18/09/30 05:09:30</a> <a title="online web tool Job" href="/projects/python/online-web-tool/" class="job">online web tool</a> <a title="Instagram credentials getting Job" href="/projects/php/Instagram-credentials-getting-fix/" class="job">Instagram credentials getting</a>
よろしくお願いしますm(__)m
できましたー(≧∇≦)b
jun68yktさま、ありがとうございます!
おかげさまでできました(^^)
# -*- coding: utf-8 -*- from bs4 import BeautifulSoup import requests import csv import re import warnings warnings.filterwarnings('ignore') r = requests.get("https://www.freelancer.com/archives/python/2018-40/") soup_content = BeautifulSoup(r.content, "html.parser") f1 = open('r.txt', 'w') f1.write(r.text) f1.close soup = BeautifulSoup(r.text) with open('pythonHref.csv', 'w+',newline='',encoding='utf-8') as f: writer = csv.writer(f, lineterminator='\n') for link in soup.find_all('a', {'href': re.compile(r'^/projects/python/')}): writer.writerow([link.get('href')])
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/14 07:53
2018/10/14 07:53
2018/10/14 07:54
2018/10/14 08:17
2018/10/14 08:25
2018/10/14 08:26