here im stuck in a problem.here i fetched users and their profile links from the site drupal.org using RegEx. but i got another links also.i want to fetch their profile links only and match the users and their profile links and show data in table is it possible in React js?
import React,{ Component } from "react"
import Axios from "axios"
class UserList extends Component {
constructor(prop) {
super(prop);
this.onChangeUserName = this.onChangeUserName.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.state = { users: null, profile:"", name:"" }
}
onChangeUserName(e) {
this.setState({ name: e.target.value })
}
onSubmit(e) {
e.preventDefault()
const userObject = {
name: this.state.name,
};
Axios.get(`https://www.drupal.org/search/user/${this.state.name}`).then(
response => {
if (response.status === 200) {
// all is good
console.log('all is good');
console.log(response);
var olList = response.data.match(/(?<=\<ol class="search-results user-results"\>\s+).*?(?=\s+\<\/ol)/gs);
var links = response.data.match(
/(\b(https?):\/\/[-A-Z0-9+&@#\\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi
);
//preg_match_all('|https?://(?:www\.)?twitter.com/#!/[a-z0-9_]+|im', $text, $matched)
this.setState({ users: olList });
this.setState({ profile: links });
} else {
console.log('something is wrong');
// something is wrong
}
}
);
}
render() {
return ( <React.Fragment>
<h1 > Search Here users of Drupal < /h1>
<form align = "center" onSubmit = { this.onSubmit } >
<input type = "search" name = "name" onChange = { this.onChangeUserName } placeholder = "Search" required / >
<button type = "submit" > Search < /button >
</form >
<h3>Relevent Search Results For Your Keywords : </h3>
<table border="2" height="100" width="300">
<tr>
<td align="center"><h2>Username</h2></td>
<td><h2>Profile Links</h2></td>
</tr>
<tr>
<td dangerouslySetInnerHTML = {
{ __html: this.state.users }
} />
<td align="char" dangerouslySetInnerHTML = {
{ __html: this.state.profile }
} />
</tr>
</table>
</React.Fragment>
);
}
}
export default UserList;
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
The first issue is that the regular expression you’re using is too broad. It’s going to match any URL within the response data, which is likely why you’re seeing unexpected results.
Let’s break this problem into multiple parts:
Fetching Usernames and Profile Links from Drupal.org: You’re currently fetching the entire HTML of the search results. Instead, you want to isolate the user names and their profile URLs.
Displaying in a Table in React: Once you have the user names and profile URLs matched, you’d likely want to display each combination in its own table row.
Let’s address the first part:
### 1. Fetching Usernames and Profile Links
To better match usernames and their profile URLs, we need to understand the structure of Drupal’s search results. Assuming that each result has a structure like:
<a href="/user-profile-link">Username</a>
A regex that captures such links would look something like:
const regex = /<a href="(\/[^"]+)">([^<]+)<\/a>/g;
With this regex:
(\/[^"]+) captures the URL.([^<]+) captures the username.Now let’s integrate this into your Axios call:
Axios.get(`https://www.drupal.org/search/user/${this.state.name}`).then(response => {
if (response.status === 200) {
const results = [];
let match;
while (match = regex.exec(response.data)) {
results.push({ link: `https://www.drupal.org${match[1]}`, name: match[2] });
}
this.setState({ users: results });
} else {
console.log('something is wrong');
}
});
### 2. Displaying in a Table in React:
Now that you have your users stored in a users state variable as an array of objects, you can map over them and create table rows:
<table border="2" height="100" width="300">
<thead>
<tr>
<th>Username</th>
<th>Profile Links</th>
</tr>
</thead>
<tbody>
{this.state.users && this.state.users.map((user, index) => (
<tr key={index}>
<td>{user.name}</td>
<td><a href={user.link} target="_blank" rel="noopener noreferrer">{user.link}</a></td>
</tr>
))}
</tbody>
</table>
This should give you a table where each row corresponds to a username and their profile link. Remember to always have a unique key prop for each row when using map in React.
Also, ensure you’re always careful when dealing with raw HTML and URLs, as there’s a potential for cross-site scripting (XSS) attacks. Ensure you sanitize any data you get from third-party websites before rendering them in your application.
Lastly, consider the ethical implications and terms of service of the website you’re scraping. Web scraping might be against the terms of service of some websites, and repeated requests might get your IP address temporarily blocked. Always respect robots.txt and be considerate with your request frequency.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.