【发布时间】:2018-02-01 21:32:48
【问题描述】:
我已经尝试查看以前关于同一主题的帖子,但似乎没有一个解决方案有效,我想确认我确实无法解决这个问题。
我是一名记者,试图从规划当局的网站上下载许可证数据。直到几个月前我才能做到这一点,但网站已经更改,在将我的代码调整到新网站后,我现在似乎每次尝试点击网站上的链接时都会收到错误 403。
任何帮助将不胜感激。
我的代码 - 不是最好看或最高效的,但我是自学成才,主要使用编码来抓取工作数据 - 页面上的统计信息:http://www.pa.org.mt/padecisionSearch?date=1/31/2018%2012:00:00%20AM
在我粘贴在下面的代码中,我试图访问每个链接许可链接(页面上的第一个链接:http://www.pa.org.mt/PACaseDetails?Systemkey=200414&CaseType=PA/10351/17%27)以获取许可详细信息。
虽然我可以毫无问题地生成链接地址(可以通过单击链接访问它们),但向该地址发送请求会返回:
b'\r\n禁止\r\n\r\n
禁止的 URL
\r\nHTTP 错误 403。请求 URL 被禁止。
\r\n\r\n'我已经尝试更改用户代理,并且我还尝试在请求之间设置一个计时器,但似乎没有任何效果。
欢迎提出建议
我的代码:
import requests
import pandas as pd
import csv
from bs4 import BeautifulSoup
from datetime import date, timedelta as td
import pandas as pd
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
import urllib
with requests.Session() as s:
#s.headers.update(head)
r= s.get("http://www.pa.org.mt",data=None, headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"})
page = (s.get("http://www.pa.org.mt/padecisionSearch?date=1/31/2018%2012:00:00%20AM", data=None, headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/63.0.3239.132 Safari/537.36"}).content)
soup = BeautifulSoup(page, 'html.parser')
search_1 = soup.find_all('table')
for item in search_1:
item1 = item.find_all('tr')
for item2 in item1:
item3 = item2.find_all('td', class_ = 'fieldData')
for element in item3:
list2.append(element.text)
zejt_number = (len(list2)/6)
zi = element.find_all('a')
if len(zi) == 0 and ((len(list2)-1)%5 == 0 or len(list2) == 1):
case_status.append("")
applicant.append("")
architect.append("")
application_type.append("")
case_category.append("")
case_officer.append("")
case_officer2.append("")
date_approved.append("")
application_link.append("")
elif len(zi) != 0:
for li in zi:
hyperlink = "http://www.pa.org.mt/"+li.get('href')
application_link.append(hyperlink)
print(hyperlink)
z = (s.get(hyperlink, data=None, headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}).content)
print(z)
【问题讨论】:
标签: beautifulsoup python-requests user-agent