PullRequest
       0

0 голосов
/ 02 марта 2020

Все, что я пытаюсь сделать, это сначала получить список ссылок всех тегов привязки, а затем очистить текст таблиц от каждого из этих очищенных списков. Но все, что я получаю, это ошибка. Кто-нибудь может мне помочь?

Мой код:

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
import csv
from datetime import datetime

pages = []
my_url = 'https://deltaimmigration.com.au/Australia-jobs/'
uClient = uReq(my_url)

page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")

col = page_soup.find('td', width="250")
# rows = table.find_all('tr')
# print(len(col))
# print(col.text)

filename = "links.txt"
f = open(filename,'w')

headers = "Link of all jobs "
f.write(headers)

for a in col.find_all('a', href=True):
   link= a['href'] 
   f_link = link.replace(".." , "https://deltaimmigration.com.au")
   pages.append(f_link)

for items in pages:
    tables = items.find('table', width="900")
    table = tables[0]

    for table in tables:
        rows_tables = table.tbody.find_all('tr')
        rows = rows_tables[0].text
        for row in rows:
            print(row.text)

f.close()

Может кто-нибудь помочь мне решить эту проблему? Разве мы не можем соскрести данные со скребущих ссылок с той же страницы? Ошибка:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:/scrape/link.py", line 8, in <module>
    uClient = uReq(my_url)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 543, in _open
    '_open', req)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10054] An existing connection was forcibly closed by the remote host>

user@Bidhya-1s MINGW64 /e/scrape
$ C:/Users/user/Anaconda3/python.exe e:/scrape/link.py
Traceback (most recent call last):
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\Users\user\Anaconda3\lib\http\client.py", line 1244, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\user\Anaconda3\lib\http\client.py", line 1290, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\user\Anaconda3\lib\http\client.py", line 1239, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\user\Anaconda3\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Users\user\Anaconda3\lib\http\client.py", line 966, in send
    self.connect()
  File "C:\Users\user\Anaconda3\lib\http\client.py", line 1414, in connect
    server_hostname=server_hostname)
  File "C:\Users\user\Anaconda3\lib\ssl.py", line 423, in wrap_socket
    session=session
  File "C:\Users\user\Anaconda3\lib\ssl.py", line 870, in _create
    self.do_handshake()
  File "C:\Users\user\Anaconda3\lib\ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:/scrape/link.py", line 8, in <module>
    uClient = uReq(my_url)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 543, in _open
    '_open', req)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\Users\user\Anaconda3\lib\urllib\request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10054] An existing connection was forcibly closed by the remote host>

'' '

1 Ответ

0 голосов
/ 02 марта 2020

В основном ваш код содержит много ошибок и совершенно нечитаем.

Я вижу, что вы используете find вместо findAll, что означает, что вы выбираете только первые url, а затем я даже не вижу ни одного запроса, который вы сделали к этому url, чтобы parse это. поэтому я не мог понять желаемую цель, так как вы не включили много информации. в любом случае, с моей точки зрения, я думаю, что мой код ниже поможет вам.

import requests
from bs4 import BeautifulSoup

main = "https://deltaimmigration.com.au/Australia-jobs/"


def First():
    r = requests.get(main)
    soup = BeautifulSoup(r.text, 'html5lib')
    links = []
    with open("links.txt", 'w', newline="", encoding="UTF-8") as f:
        for item in soup.findAll("td", {'width': '250'}):
            item = item.contents[1].get("href")[3:]
            item = f"https://deltaimmigration.com.au/{item}"
            f.write(item+"\n")
            links.append(item)
    print(f"We Have Collected {len(links)} urls")
    return links


def Second():
    links = First()
    with requests.Session() as req:
        for link in links:
            print(f"Extracting {link}")
            r = req.get(link)
            soup = BeautifulSoup(r.text, 'html5lib')
            for item in soup.findAll("table", {'width': '900'}):
                print("*" * 40)
                print(item.text)
                print("*" * 40)


Second()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...